﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JLL.China.Infrastructure.Repositories;
using JLL.China.Infrastructure.Repositories.LINQ2SQL;
using JLL.China.Infrastructure.RepositoryFramework;
using JLL.China.Infrastructure.UnitOfWork;
using JLL.China.Model.DTO;
using JLL.China.Infrastructure.Repositories.ProjectDirectoryMedia;
using ProjectDirectoryMedia = JLL.China.Model.ProjectDirectoryMedia;

namespace JLL.China.Infrastructure.Services
{
   public class ProjectDirectoryMediaService
    {
         private static ProjectDirectoryMediaRepository _repository;
         public int ID { get; set; }
         static ProjectDirectoryMediaService()
        {
            _repository = RepositoryFactory.GetRepository<ProjectDirectoryMediaRepository, Model.ProjectDirectoryMedia, int>(new LINQ2SQLUnitOfWork<int>());
        }
        public static void Create(ProjectDirectoryMedia item)
        {
            _repository.Add(item);
        }
        public static void Delete(ProjectDirectoryMedia item)
        {
            _repository.Remove(item);
             _repository.UnitOfWork.Commit();
        }
        public static void Save()
        {
            _repository.UnitOfWork.Commit();
        }
        public static int getCountItem()
        {
            return _repository.CountItem;
        }
        public static IList<ProjectDirectoryMedia> GetListImage(int id)
        {
            return _repository.FindById(id);
        }
        public static Model.ProjectDirectoryMedia GetProjectDirectoryMediaById(int id)
        {
            return _repository.FindBy(id);
        }

        public static decimal FindByListingID(decimal id,string imgType)
        {
            return _repository.FindByListingIDImageType(id,imgType);
        }
        public static void Update(Model.ProjectDirectoryMedia item)
        {
            var media = GetProjectDirectoryMediaById(item.ID);
            if (item.IsActive == null || item.IsDefault == null)
            {
                item.IsActive = media.IsActive;
                item.IsDefault = media.IsDefault;
            }
            else
            {
                media.IsActive = item.IsActive;
                media.IsDefault = item.IsDefault;
            }
            _repository[media.ID] = media;

            _repository.UnitOfWork.Commit();
        }
        public static int GetID(Guid guid)
        {
            return _repository.GetID(guid);
        }


        /// <summary>
        /// update sequence for Project directory media
        /// </summary>
        /// <param name="bannerid"></param>
        /// <param name="pos"></param>
        public static void UpdateOrder(decimal prjId, int pos)
        {
            using (var dbc = new JLLCHINADataContext())
            {
                var lb = from c in dbc.ProjectDirectoryMedias
                         where c.Media_ID.Equals(prjId)
                         select c;

                foreach (var b in lb)
                {

                    b.Orders = pos;

                    dbc.SubmitChanges();

                }
            }
        }
    }
    
}
